package com.handinglian.employee.controller;

import com.apidoc.annotation.Api;
import com.apidoc.annotation.ApiVersion;
import com.handinglian.common.dto.ResultData;
import com.handinglian.common.factory.ResultDataFactory;
import com.handinglian.employee.dto.DepartmentDto;
import com.handinglian.employee.entity.Department;
import com.handinglian.employee.param.DepartmentCreateParam;
import com.handinglian.employee.param.DepartmentOrderParam;
import com.handinglian.employee.param.DepartmentRecoverParam;
import com.handinglian.employee.param.DepartmentUpdateParam;
import com.handinglian.employee.service.DepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("部门")
@RestController
@RequestMapping("/{version}/department")
@ApiVersion(1.0)
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 创建部门
     */
    @RequiresPermissions("employee_list:create")
    @PostMapping("/createDepartment")
    public ResultData createDepartment(@RequestBody DepartmentCreateParam departmentCreateParam){
        Department department = departmentService.loadInvalidDepartment(departmentCreateParam.getDepartmentName());
        if (department != null){
            return ResultDataFactory.generateExistInDeleteResultData();
        } else {
            int amount = departmentService.createDepartment(departmentCreateParam);
            return ResultDataFactory.generateResultData(amount);
        }
    }

    /**
     * 从删除列表中恢复部门
     */
    @RequiresPermissions("employee_list:update")
    @PutMapping(value = "/recoverDepartment")
    public ResultData recoverDepartment(@RequestBody DepartmentRecoverParam departmentRecoverParam) {
        int amount = departmentService.recoverDepartment(departmentRecoverParam.getDepartmentName());
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 删除部门
     */
    @RequiresPermissions("employee_list:delete")
    @DeleteMapping(value = "/deleteDepartment")
    public ResultData deleteDepartment(Integer departmentId) {
        int amount = departmentService.deleteDepartment(departmentId);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 更新部门
     */
    @RequiresPermissions("employee_list:update")
    @PutMapping(value = "/updateDepartment")
    public ResultData updateDepartment(@RequestBody DepartmentUpdateParam departmentUpdateParam) {
        int amount = departmentService.updateDepartment(departmentUpdateParam);
        return ResultDataFactory.generateResultData(amount);
    }


    /**
     * 查询部门
     */
    @RequiresPermissions("employee_list:list")
    @GetMapping(value = "/loadDepartment")
    public ResultData<DepartmentDto> loadDepartment(Integer departmentId) {
        DepartmentDto department = departmentService.loadDepartment(departmentId);
        return ResultDataFactory.generateSuccessResultData(department);
    }

    /**
     * 获取部门列表(common)
     */
    @RequiresPermissions("employee_list:list")
    @GetMapping("/inquireDepartmentList")
    public ResultData<DepartmentDto> inquireDepartmentPageList() {
        List<DepartmentDto> departmentList = departmentService.inquireDepartmentList();
        return ResultDataFactory.generateSuccessResultData(departmentList);
    }

    /**
     * 更新部门顺序
     */
    @RequiresPermissions("employee_list:update")
    @PostMapping("/updateOrderNo")
    public ResultData<DepartmentDto> updateOrderNo(@RequestBody DepartmentOrderParam departmentOrderParam) {
        int amount = departmentService.updateOrderNo(departmentOrderParam.getUpdatedOrderNos(), departmentOrderParam.getDepartmentIds());
        return ResultDataFactory.generateSuccessResultData(amount);
    }

    /**
     * 测试专用
     */
    @PostMapping("/test")
    public ResultData test() {
        return ResultDataFactory.generateResultData(null);
    }
}
